<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/6
 * Time: 14:49
 */

namespace app\common\traits;

use app\admin\logic\Auth;
use app\admin\model\State;
use app\admin\model\AuthRule;
use think\Db;

/**
 * Trait Gate 当前系统权限判断
 * @package app\common\traits
 * @author: Huang
 */
trait Gate
{
    public static $auth; //服务层对象
    private static $uid; //服务层对象
    private static $authrule; //服务层对象

    /**
     * 静态方法，单例统一访问入口
     * @return Auth
     * @author: Huang
     * @internal
     */
    static public function getAuth()
    {
        if (is_null(self::$auth) || !isset (self::$auth)) {
            self::$auth = Auth::instance();
        }
        return self::$auth;
    }

    /**
     * 静态方法，单例统一访问入口
     * @return Auth
     * @author: Huang
     * @internal
     */
    static public function getAuthRules()
    {
        if (is_null(self::$authrule) || !isset (self::$authrule)) {
            self::$authrule = new AuthRule();
        }
        return self::$authrule;
    }

    /**
     * 静态方法，单例统一访问入口
     * @return Auth
     * @author: Huang
     * @internal
     */
    static public function getUid()
    {
        if (is_null(self::$uid) || !isset (self::$uid)) {
            self::$uid = is_login();
        }
        return self::$uid;
    }

    /**
     * 判断该用户有该角色
     * @param $role 用户标识
     * @param string $uid
     * @return bool
     * @internal
     */
    public function hasRole($role, $uid = '')
    {
        if (empty($role)) return false;
        if (empty($uid)) $uid = self::getUid();
        $group = self::getAuth()->getUserGroup($uid);
        $access = [];
        foreach ($group as $v) {
            $access[] = $v['mark'];
        }
        if (is_array($role)) {
            $temp = array_intersect($role, $access);
            if (!empty($temp)) return true;
        } else {
            if (in_array($role, $access)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断用户是否有某一权限 例如 admin/index/index,edit_art.
     * @param string $rule
     * @return bool
     * @internal
     */
    public function checkRule($rule)
    {
        return self::getAuthRules()->check_auth($rule);
    }

    /**
     * 获取用户状态id
     * @param string $uid
     * @return array
     * @internal
     */
    public function getUserGroupStatus($uid = '')
    {
        if (empty($uid)) $uid = self::getUid();
        $group = self::getAuth()->getUserGroup($uid);
        $access = '';
        foreach ($group as $v) {
            $access += $v['hidstates'] . ',';
        }
        return explode(',', $access);
    }

    /**
     * 获取当前登录人员能编辑的状态id
     * @return array
     * @Author 蒋
     * @internal
     */
    public function getCanEditStates()
    {
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色的ids
        $caneditstates = Db::name('auth_group')->where(['id' => ['in', $authgroupids]])->field('caneditstates')->select();
        return strToArr($caneditstates, 'caneditstates');
    }

    /**
     * 编辑时可以选中的状态
     * @return array
     * @author: Huang
     * @internal
     */
    public function getEditStates()
    {
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $editstates = Db::name('auth_group')->where(['id' => ['in', $authgroupids]])->field('editstates')->select();
        return strToArr($editstates, 'editstates');
    }

    /**
     * 用户查看时可以看到电话的状态id
     * @return array
     * @author: Huang
     * @internal
     */
    public function getHidStates()
    {
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $hidstates = Db::name('auth_group')->where(['id' => ['in', $authgroupids]])->field('hidstates')->select();
        return strToArr($hidstates, 'hidstates');
    }

    /**
     * 当前角色是否能查看订单的电话
     * @return mixed
     * @author: Huang
     * @internal
     */
    public function getBaomiqx()
    {
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $baomi = Db::name('auth_group')->where(['id' => ['in', $authgroupids]])->field('baomi')->cache(60000)->find();
        return $baomi['baomi'];
    }
}